<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Auth_model extends CI_Model {

    function login($email, $password) {
        $this->load->library('bcrypt');
        $query = $this->db->where('member_email', $email)
                ->get('members');
        if ($query->num_rows() == 1) {
            $row = $query->row_array();
            if ($this->bcrypt->check_password($password, $row['member_password'])) {
                return $row;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Check is Backend Logged in
     */
    function is_loggedin() {
        if ($member_id = $this->input->cookie('remember', false)) {
             $this->load->library('encrypt');
            $row = $this->login_by_cokie($this->encrypt->decode($member_id));

            $sess_array = array(
                'member_id' => $row['member_id'],
                'member_email' => $row['member_email'],
                'member_firstname' => $row['member_firstname'],
                'member_lastname' => $row['member_lastname'],
                'member_thumb' => $row['member_thumb']
            );
            $this->session->set_userdata('is_loggedin', $sess_array);
        }
        $is_loggedin = $this->session->userdata('is_loggedin');


        if ($is_loggedin) {

            return TRUE;
        }
        return FALSE;
    }
    
    function is_admin() {
        $session = $this->session->userdata('is_loggedin');

        if (isset($session['member_role'])&&$session['member_role']=='admin') {

            return TRUE;
        }
        return FALSE;
    }

    function login_by_cokie($member_id) {
        
        $query = $this->db->where('member_id', $member_id)
                ->get('members');

        if ($query->num_rows() == 1) {
            $row = $query->row_array();
            return $row;
        } else {
            return false;
        }
    }

    /**
     * Logout
     */
    function logout() {
        $this->session->unset_userdata('is_loggedin');
        return TRUE;
    }

    /**
     * Officer data
     */
    function member_detail() {
        $session_data = $this->session->userdata('is_loggedin');
        $data = array(
            'member_id' => $session_data['member_id'],
            'member_email' => $session_data['member_email'],
            'member_firstname' => $session_data['member_firstname'],
            'member_lastname' => $session_data['member_lastname']
        );

        return $data;
    }
    

    function do_fb_login($accessToken) {
        $url = 'https://graph.facebook.com/me?access_token=' . $accessToken;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
        curl_close($ch);
        $user = json_decode($response);

        print_r($user);
    }

}
